#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

ListNode* makeList(vector<int> vec) {
    if (vec.empty()) return nullptr;
    ListNode* head = new ListNode(0);
    ListNode* tail = head;
    for (const auto& v : vec) {
        tail->next = new ListNode(v);
        tail = tail->next;
    }
    tail->next = nullptr;
    tail = head->next;
    delete head;
    return tail;
}

void printList(ListNode* head) {
    while (head) {
        cout << head->val << " -> ";
        head = head->next;
    }
    cout << endl;
}